package com.ruoyi.dictionaries.service;

import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.dictionaries.domain.StudentStatusChange;
import com.ruoyi.dictionaries.domain.vo.StatusChangeVo;
import com.ruoyi.dictionaries.mapper.StudentStatusChangeMapper;
import org.openoa.base.constant.enums.ButtonTypeEnum;
import org.openoa.base.interf.ActivitiService;
import org.openoa.base.interf.ActivitiServiceAnno;
import org.openoa.base.interf.FormOperationAdaptor;
import org.openoa.base.util.SecurityUtils;
import org.openoa.base.vo.BpmnStartConditionsVo;
import org.openoa.base.vo.BusinessDataVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@ActivitiServiceAnno(svcName = "StudentChange_ShenQing",desc = "学籍异动申请")
@Service("StudentChange_ShenQing")
public class XueJiYiDong implements FormOperationAdaptor<StatusChangeVo>, ActivitiService {

    @Autowired
    private StudentStatusChangeMapper studentStatusChangeMapper;

    @Override
    public BpmnStartConditionsVo previewSetCondition(StatusChangeVo statusChangeVo) {
        String userId = statusChangeVo.getStartUserId();
        return BpmnStartConditionsVo.builder()
                .startUserId(userId)
                .build();
    }

    @Override
    public void initData(StatusChangeVo statusChangeVo) {
        // 初始化数据，可设置默认值
        statusChangeVo.setApplyDate(new Date());
        statusChangeVo.setMaterialsStatus("待审核");
        statusChangeVo.setReviewer1Result("待审核");
        statusChangeVo.setReviewer2Result("待审核");
        statusChangeVo.setReviewer3Result("待审核");
    }

    @Override
    public BpmnStartConditionsVo launchParameters(StatusChangeVo statusChangeVo) {
        String userId = statusChangeVo.getStartUserId();
        return BpmnStartConditionsVo.builder()
                .startUserId(userId)
                .build();
    }

    @Override
    public void queryData(StatusChangeVo statusChangeVo) {
        // 通过businessId查询对应的学籍异动记录
        StudentStatusChange statusChange = studentStatusChangeMapper.selectStudentStatusChangeById(statusChangeVo.getBusinessId());
        if (statusChange != null) {
            BeanUtils.copyProperties(statusChange, statusChangeVo);
        }
    }

    @Override
    public void submitData(StatusChangeVo statusChangeVo) {
        StudentStatusChange statusChange = new StudentStatusChange();
        BeanUtils.copyProperties(statusChangeVo, statusChange);
        statusChange.setId(IdUtils.fastSimpleUUID());
        // 设置创建信息
        statusChange.setCreateTime(new Date());
        statusChange.setCreateBy(SecurityUtils.getLogInEmpNameSafe());
        statusChange.setApplyDate(new Date());
        // 保存数据
        studentStatusChangeMapper.insertStudentStatusChange(statusChange);
        // 可以根据需要添加其他业务逻辑
        // 设置工作流相关参数
        statusChangeVo.setBusinessId(statusChange.getId());
        statusChangeVo.setProcessTitle("学籍异动申请-" + statusChange.getStudentName());
        statusChangeVo.setProcessDigest(statusChange.getChangeType() + "-" + statusChange.getChangeReason());
        statusChangeVo.setEntityName(StudentStatusChange.class.getSimpleName());
    }

    @Override
    public void consentData(StatusChangeVo statusChangeVo) {
        // 处理重新提交的情况
        if (ButtonTypeEnum.BUTTON_TYPE_RESUBMIT.getCode().equals(statusChangeVo.getOperationType())) {
            StudentStatusChange statusChange = new StudentStatusChange();
            BeanUtils.copyProperties(statusChangeVo, statusChange);
            statusChange.setId(statusChangeVo.getBusinessId());
            statusChange.setUpdateTime(new Date());
            statusChange.setUpdateBy(SecurityUtils.getLogInEmpNameSafe());

            studentStatusChangeMapper.updateStudentStatusChange(statusChange);
        }
        // 可以根据需要添加审核通过时的处理逻辑
    }

    @Override
    public void backToModifyData(StatusChangeVo statusChangeVo) {
        // 退回修改时的处理逻辑
        StudentStatusChange statusChange = studentStatusChangeMapper.selectStudentStatusChangeById(statusChangeVo.getBusinessId());
        if (statusChange != null) {
            // 可以根据需要设置退回状态
            statusChange.setUpdateTime(new Date());
            statusChange.setUpdateBy(SecurityUtils.getLogInEmpNameSafe());
            studentStatusChangeMapper.updateStudentStatusChange(statusChange);
        }
    }

    @Override
    public void cancellationData(StatusChangeVo statusChangeVo) {
        // 取消申请时的处理逻辑
        // 这里可以根据业务需求执行删除或更新状态的操作
    }

    @Override
    public void finishData(BusinessDataVo businessDataVo) {
        // 流程完成时的处理逻辑
        // 可以在这里设置最终状态
        if (businessDataVo instanceof StatusChangeVo) {
            StatusChangeVo statusChangeVo = (StatusChangeVo) businessDataVo;
            StudentStatusChange statusChange = studentStatusChangeMapper.selectStudentStatusChangeById(statusChangeVo.getBusinessId());
            if (statusChange != null) {
                statusChange.setFinalStatus("已完成");
                statusChange.setUpdateTime(new Date());
                statusChange.setUpdateBy(SecurityUtils.getLogInEmpNameSafe());
                studentStatusChangeMapper.updateStudentStatusChange(statusChange);
            }
        }
    }
}
